Welcome Guest | Sign in | Register

Home > Java Programming > Flow Control > Questions and Answers

01. class A{
Public static void main(String args[]){
System.out.println(i);
int I;

}
}
A. compile time error B. run time error
C. no output D. 10

Answer and Explanation

Answer: compile time error

Explanation:
here I variable is a local variable , local variable should be initialized before the use , but they are trying to use before declaring ,that’s why it is throwing compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. class A{
Public static void main(String args[]){
Int i=10;
System.out.println(i);
int j=20;
System.out.println(j);
}
}
A. compile time error B. run time error
C. no output D. 10 20

Answer and Explanation

Answer: 10 20

Explanation:
here I and j variable is a local variable , local variable should be initialized before the use , here they are initializing and using it so it will display normal output.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. class A{
Public static void main(String args[]){
Int i;
System.out.println(“hello”);
}
}
A. compile time error B. run time error
C. hello D. no output

Answer and Explanation

Answer: hello

Explanation:
here I variable is a local variable , local variable should be initialized before the use , here they not using it they just declared it,so it will display normal output, it will not show any error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Find the missing snippet in the below code :

1. public class Test {
2. public static void main(String [ ] args) {
3.
4. switch(a) {
5. default:
6. System.out.println("You choose correct option");
7. } } }

Which given below snippet is best for inserting at line number 3 to compile the above code successfully :-
A. int a = 3; 
short a = 4;
Boolean a = true;
B. char a = 'p'; 
short a = 4;
long a= 388;
C. char a = 'p';
int a = 3;
short a = 4;
D. long a= 388;
Boolean a = true;
short a = 4;

Answer and Explanation

Answer: char a = 'p';

int a = 3;
short a = 4;

Explanation:
Because in this code switch() method only allows integer ,short , character and enum constants as argument. The "long" data type & "Boolean" is not permissible as argument to 'switch()'.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What is the Output of below code after compilation :

1 .public class Question3{
2 .public static void main(String[] args) {
3 .try {
4 .int a= (int)(Math.random()*5);
5 .if(a<=2.5)
6 .System.out.println("a="+a);
7 .else
8 .throw new Exception("a>2.5");
9. } catch (Exception e){

10. System.err.println(e.getMessage() );
11. System.err.println("Value of a="+a);
12. } } }

Find the output of the following code :
A. An Exception is raised during compilation. B. An Exception is thrown at run time.
C. Unresolved compilation problem. D. a=2 or a=1 or i >2.5 Value of i =null

Answer and Explanation

Answer: Unresolved compilation problem.

Explanation:
This code will not compile because variable "a" at line number 11 , is not recognize by compiler since "a" is a local variable declared inside "Try" block. This code can be corrected by applying code line "static String a = null ;"at line number2(or immediately after declaration of class & before 'main( )').

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Find the Output Of the following Code:

1. import java.io.*;
2. public class Example4 {
3. public static void main(String[] args) {
4. Example4 e4 = new Example4();
5. try{
6. e4.check();}
7. catch(IOException e){}}
8. void check() throws IOException{
9. System.out.println("Inside check() method of Class ");
10. throw new IOException();}}
11. class Subexample4 extends Example4 {
12. void check() {
13. System.out.println("Inside check() Method of Subclass");
14. }}

What will be the output of the following code snippet :
A. Above program will throw exception & will not run. B. Inside check() method of class
C. Inside check() method of Subclass D. Inside check() method of class
Inside check() method of Subclass.

Answer and Explanation

Answer: Inside check() method of class

Explanation:
Because the "e4"object is the member of the main class, therefore it can call only the methods of the main class. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. What should be at line number 3 to get the total sum of array "sum" ?

public int totalsum( int[] sum ){
int a, b= 0 ;
//which 'for' loop should be here(line :3)
{
b += sum[ a++ ] ;
}
return b ;
}

Which 'for' loop should be at line number 3 to calculate total sum of the array "sum" :
A. for( int a = 0 ; a< sum.length ; )     B. for( a= 0 ; a< sum.length ; )    
C. for( a = 0 ; a< sum.length ; a++ )     D. for( a = 1 ; i <= sum.length ; a++ ) 

Answer and Explanation

Answer: for( a= 0 ; a< sum.length ; )    

Explanation:
In 1st option, variable a is declared again i.e. duplicate variable.
In 3rd option, due to extra loop iterator "a++" , the summation would not be correct.
In 4th option. due to initialization a=1 + due to extra loop iterator "a++" , the summation would not be correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. Which are correct input-output options :

public class Example6{
public static void main(String args[])
{
public int check(char a) {
if (a <= 'N') {
if (a == 'E')
return 2;
return 1;
} else if (a == 'S')
return 3;
else if (a == 'W')
return 4;
return 0; }
}
}

Options are :
A. a='A' output=1.
Compile error.
B. Compile error.
a='X' output=0,
C. a='X' output=0,
a='A' output=1.

D. a='A' output=1.
a='D' output=0.

Answer and Explanation

Answer: a='X' output=0,

a='A' output=1.

Explanation:
Since 'Ascii' value of 'D' is less than 'Ascii' value of 'N', so it's output will be 1.That's why it(4) is not correct option to choose.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. Which one is correct option : 

1. public class Example7 {
2. public static void main(String args[]) {
3 . Example7 e7 = new Example7();
4. int b = ? ;
5. e7.Movie(b);
6. }
7. public void Movie(int q) {
8. switch (q) {
9. case 1:
10. System.out.print("me ");
11. case 2:
12 .System.out.print("myself ");
13 .break;
14. case 3:
15. System.out.print("Irene ");
16. default:
17. System.out.print("Movie");
18. }}}

Which input-output option is correct :
A. a = 4, Output = " Movie"
a=3,Output="Irene Movie"
B. a=0, Output="me myself"
a = 4, Output = " Movie"
C. a=3,Output="Irene Movie"
a= 1, Output = " me"
D. a= 1, Output = " me"
a = 4, Output = " Movie"

Answer and Explanation

Answer: a = 4, Output = " Movie"

a=3,Output="Irene Movie"

Explanation:
Since break is in between case1 & case 2 , the case switch will continue until it meets to break or end of the program. Input 0 & 1 produce -'Movie' & 'me myself' respectively.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. What is the output of the following code:

public class Example10 {
public static void main(String[] argv) {
SubExample2 se = new SubExample2();
se.showTitle();

}
}

class SubExample1{
String Name;

public SubExample1(String n) {
Name = n;
}

public void showName() {
System.out.println("Name is " + Name);
}
}

class SubExample2 extends SubExample1 {
public void setName(String nn) {
Name = nn;
}
}

What is the output of the above given code :
A. n B. nn
C. null D. Unresolved compilation problem:

Answer and Explanation

Answer: Unresolved compilation problem:

Explanation:
Since constructor of the SubExample1 class has a parameterized constructor that's why the "Name" value is undefined in this case.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.